package com.zq.ssmdemo.controller;


import com.github.pagehelper.PageInfo;
import com.zq.ssmdemo.pojo.DtoUpdateLL;
import com.zq.ssmdemo.pojo.Employees;
import com.zq.ssmdemo.service.IEmployeesService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author 张乔
 * @since 2024-06-09
 */
@Controller
@RequestMapping("/employees")
public class EmployeesController {

    @Resource
    private IEmployeesService employeesService;


    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model){
        System.out.println(username+password);
employeesService.login(username,password);
        model.addAttribute("username", username);

        return "loginSuccess";
    }

    @PostMapping("/register")
    public String register(@ModelAttribute("formData") Employees employees){
        System.out.println(employees);
employeesService.register(employees);
        return "index";
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(  @RequestBody  Employees employees){
        System.out.println(employees);

        employeesService.registersss(employees);
        return "index";
    }



    @GetMapping("/to/employees")
    public String toEmployess(Model model){
//     PageInfo<Employees> pageInfo= employeesService.getAllEmployees();

//     model.addAttribute("pageInfo", pageInfo);
        return "employees";
    }

    @ResponseBody
@GetMapping("/getPage/{page}/{limit}")
    public PageInfo<Employees> getPage(@PathVariable("page") int page,
                                      @PathVariable("limit") int limit) {
        PageInfo<Employees> pageInfo = employeesService.getPageEmployees(page, limit);
        return pageInfo;
    }
    @ResponseBody
@PutMapping("/update")
    public String update(@RequestBody DtoUpdateLL employees){

    employeesService.update(employees);
        return "success";

}
    @ResponseBody
    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") int id){
        employeesService.delete(id);
        return "success";
    }


}
